// http服务, 在此处注册路由信息
package http

import (
	"context"
	"gitee.com/legend9999/publisher/conf"
	"log"
	"net/http"
	"os"
	"os/signal"
	"time"
)

func Start() {
	srv := &http.Server{
		Addr:    conf.SysConf.GetAppPort(),
		Handler: initRouter(),
	}
	go func() {
		// 连接服务器
		if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
			log.Fatalf("listen: %s\n", err)
		}
	}()

	// 等待中断信号超时5秒正常关闭服务器
	quit := make(chan os.Signal)
	signal.Notify(quit, os.Interrupt)
	<-quit
	log.Println("Shutdown Server ...")

	ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
	defer cancel()
	if err := srv.Shutdown(ctx); err != nil {
		log.Fatal("Server Shutdown:", err)
	}
	log.Println("Server exiting")

}
